package cn.laike.modules.dev.controller;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;

import cn.laike.common.exception.RRException;
import cn.laike.common.utils.FileUtils;
import cn.laike.common.utils.PageUtils;
import cn.laike.common.utils.R;
import cn.laike.common.validator.ValidatorUtils;
import cn.laike.modules.dev.entity.HotelEntity;
import cn.laike.modules.dev.entity.HotelRoomEntity;
import cn.laike.modules.dev.service.HotelRoomService;
import cn.laike.modules.dev.service.HotelService;
import cn.laike.modules.dev.utils.DevUtils;
import cn.laike.modules.sys.shiro.ShiroUtils;



/**
 * 酒店房型
 *
 * @author ziye
 * @email 524748907@qq.com
 * @date 2019-02-18 16:57:21
 */
@RestController
@RequestMapping("dev/hotelroom")
public class HotelRoomController {
	@Autowired
	private HotelService hotelService;
	
    @Autowired
    private HotelRoomService hotelRoomService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dev:hotelroom:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = hotelRoomService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dev:hotelroom:info")
    public R info(@PathVariable("id") Integer id){
        HotelRoomEntity hotelRoom = hotelRoomService.selectById(id);

        return R.ok().put("hotelRoom", hotelRoom);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dev:hotelroom:save")
    public R save(@RequestBody HotelRoomEntity hotelRoom){
    	HotelEntity hotel = hotelService.selectById(hotelRoom.getHotelId());
    	if(hotel == null) {
    		return R.error("请到'酒店列表'->'房型管理'添加！");
    	}
    	hotelRoom.setConfigId(hotel.getConfigId());
    	hotelRoom.setConfigName(hotel.getConfigName());
    	hotelRoom.setHotelId(hotel.getId());
    	hotelRoom.setHotelName(hotel.getName());
    	
    	hotelRoom.setUid(hotel.getUid());
    	hotelRoom.setDelFlag("0");
    	hotelRoom.setCreateTime(new Date());
    	
        hotelRoomService.insert(hotelRoom);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dev:hotelroom:update")
    public R update(@RequestBody HotelRoomEntity hotelRoom){
        ValidatorUtils.validateEntity(hotelRoom);
        hotelRoomService.updateAllColumnById(hotelRoom);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @GetMapping("/queryHotelRoomNameList")
    public R queryHotelRoomNameList(Integer hotelId){
    	System.out.println(hotelId);
    	List list = hotelRoomService.queryHotelRoomNameList(hotelId);
        return R.ok().put("list", list);
    }
    
	@RequestMapping("/upload")
	public R upload(@RequestParam("picfile") MultipartFile picfile,HttpServletRequest request) throws Exception {
		if (picfile.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
		String url = "";
		if(picfile != null && picfile.getOriginalFilename().length()>4){
			String picFilePath = DevUtils.getHotelRoomResourceSavePath()+File.separator;
			FileUtils.createDirectory(picFilePath);
			String sname = picfile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = "room"+ShiroUtils.getUserId() + "_"+System.currentTimeMillis()+fileType;
			File file = new File(picFilePath+fname);
			System.out.println("保存文件:"+picFilePath+fname);
			try {
				picfile.transferTo(file);
				url = file.getName();
			} catch (Exception e) {
				System.out.println("上传文件失败:"+picFilePath+fname);
				e.printStackTrace();
			}
		}
		return R.ok().put("url", url);
	}

}
